package com.boyle.musicplayer;

import java.util.ArrayList;
import java.util.HashMap;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.boyle.util.adapter.LazyAdapter;
import com.boyle.utils.XMLParser;

public class MainActivity extends Activity {
	// 所有的静态变量
	// XML文件地址,打开地址看一下
	static final String URL = "http://api.androidhive.info/music/music.xml";
	// XML 节点
	public static final String KEY_SONG = "song"; // parent node
	public static final String KEY_ID = "id";
	public static final String KEY_TITLE = "title";
	public static final String KEY_ARTIST = "artist";
	public static final String KEY_DURATION = "duration";
	public static final String KEY_THUMB_URL = "thumb_url";

	ListView list;
	LazyAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		// 创建一个线程来处理下载文件操作
		new Thread(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				Message msg = new Message();
				msg.what = 0;
				
				//获取歌曲列表
				msg.obj = getList();
				handler.sendMessage(msg);
			}
		}).start();
	}

	private Handler handler = new Handler(){
		public void handleMessage(Message msg) {
			switch(msg.what){
			case 0 :
				list = (ListView) findViewById(R.id.list);
				
				ArrayList<HashMap<String, String>> songsList = (ArrayList<HashMap<String, String>>)msg.obj;
				adapter = new LazyAdapter(MainActivity.this, songsList);
				list.setAdapter(adapter);

				// 将监听器的对象绑定到列对象上面
				list.setOnItemClickListener(new ListViewItemOnClickListener());
				break;
			}
		}
	};
	
	// 为ListView列添加单击事件
	class ListViewItemOnClickListener implements OnItemClickListener {
		@Override
		public void onItemClick(AdapterView<?> parent, View view,
				int position, long id) {
			
			System.out.println("----->OnClick");
			// 这里可以自由发挥，比如播放一首歌曲等等
		}
	}
	
	// 获取歌曲列表
	private ArrayList<HashMap<String, String>> getList() {
		ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();

		XMLParser parser = new XMLParser();
		String xml = parser.getXmlFromUrl(URL); // 从网络获取XML
		Document doc = parser.getDomElement(xml); // 获取 DOM 节点

		NodeList nl = doc.getElementsByTagName(KEY_SONG);
		// 循环遍历所有的歌节点 <song>
		for (int i = 0; i < nl.getLength(); i++) {
			// 新建一个 HashMap
			HashMap<String, String> map = new HashMap<String, String>();
			Element e = (Element) nl.item(i);
			// 每个子节点添加到HashMap关键= >值
			map.put(KEY_ID, parser.getValue(e, KEY_ID));
			map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
			map.put(KEY_ARTIST, parser.getValue(e, KEY_ARTIST));
			map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
			map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));

			// HashList添加到数组列表
			songsList.add(map);
		}
		return songsList;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
